<?php
/* explain:		
 * Project:     
 * File:        vedioUpload.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");
include_once("constant/CommonClass.php");

class ImageCustomImpl extends CommonUtils
{
	
	function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag = $DBTag;
	}

	public function getById($id) 
	{
		$sql = "SELECT width, height, extName, md5, fileSize, name, description, status, level, isAnimated FROM image_custom WHERE id = $id";
		
		$dto 	= null;
		$conn 	= null;
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$dto = new ImageDto();

				$dto->id 			= $id;
				$dto->width			= $row[0];
				$dto->height		= $row[1];
				$dto->extName		= $row[2];
				$dto->md5			= $row[3];
				$dto->fileSize		= $row[4];
				$dto->name			= $row[5];
				$dto->description	= $row[6];
				$dto->status		= $row[7];
				$dto->level			= $row[8];
				$dto->isAnimated	= $row[9];
			} else {
				$this->message = "get data failed by Id. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		if($conn != null)
			$this->freeConnection($this->DBTag, $conn);
			
		return $dto;
	}
	
	public function getByMd5($md5) 
	{
		$sql = "SELECT width, height, extName, fileSize, name, description, id, status, level, isAnimated FROM image_custom WHERE md5 = '$md5'";
		
		$dto = null;
		$conn = null;
//		echo $sql . "\n";
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$dto = new ImageDto();

				$dto->width			= $row[0];
				$dto->height		= $row[1];
				$dto->extName		= $row[2];
				$dto->fileSize		= $row[3];
				$dto->name			= $row[4];
				$dto->description	= $row[5];
				$dto->id 			= $row[6];
				$dto->status		= $row[7];
				$dto->level			= $row[8];
				$dto->isAnimated	= $row[9];
				
				$dto->md5			= $md5;				
			} else {
				$this->message = "get data failed by Id. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
			
		return $dto;
	}
	
	/* 
	 *
	 * @param  string 	name 
 	 * @return int    	true: insert ID
	 */
    public function add($dto)
	{
		if($dto instanceof ImageDto){			
			$sql 	 = "INSERT INTO image_custom SET width='";
			$sql	.= $dto->width;
	
			$sql 	.= "', height='";
			$sql	.= $dto->height;
			
			$sql	.= "', md5='";
			$sql	.= $dto->md5;
			
			$sql	.= "', fileSize='";
			$sql	.= $dto->fileSize;
			
			$sql	.= "', extName='";
			$sql	.= $dto->extName;
			
			if(!empty($dto->name)){
				$sql 	.= "', name='";
				$sql	.= addslashes($dto->name);
			}
			
			if(!empty($dto->description)){
				$sql 	.= "', description='";
				$sql	.= addslashes($dto->description);
			}
			
			$sql	.= "', status=";
			$sql	.= $dto->status;

			$sql	.= ", level=";
			$sql	.= $dto->level;
			
			$sql	.= ", isAnimated=";
			$sql	.= $dto->isAnimated;
			
			$id = $this->insert($this->DBTag, $sql);
			
			$dto->id = $id;
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of ImageDto.';
		}
			
		return $id;
	}
	
	/* modify
	 *
	 * @param  string 	name 
 	 * @param  return 	boolean
	 */
	public function edit($dto)
	{
		$isOk = false;
		
		if($dto instanceof ImageDto){			
			$sql 	 = "UPDATE image_custom SET width='";
			$sql	.= $dto->width;
	
			$sql 	.= "', height='";
			$sql	.= $dto->height;
			
			$sql	.= "', md5='";
			$sql	.= $dto->md5;
			
			$sql	.= "', fileSize='";
			$sql	.= $dto->fileSize;
			
			$sql	.= "', extName='";
			$sql	.= $dto->extName;
			
			if(!empty($dto->name)){
				$sql 	.= "', name='";
				$sql	.= addslashes($dto->name);
			}
			
			if(!empty($dto->description)){
				$sql 	.= "', description='";
				$sql	.= addslashes($dto->description);
			}

			$sql	.= "', status=";
			$sql	.= $dto->status;
			
			$sql	.= ", level=";
			$sql	.= $dto->level;
			
			$sql	.= ", isAnimated=";
			$sql	.= $dto->isAnimated;
			
			$sql	.= " WHERE id =";
			$sql	.= $dto->id;
			
			$updateCount = $this->update($this->DBTag, $sql);
			
			$isOk = true;			
		}else{			
			$this->message = 'input object is not instance of ImageDto.';
		}		

		return $isOk;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM image_custom WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function isExistedByMd5($md5)
	{
		$sql = "SELECT id FROM image_custom WHERE md5 = '$md5'";
		
		return $this->isExisted($this->DBTag, $sql);
	}
	
	public function getCustoms($where, $order, $page = 0, $discount = 20)
	{
		$arrback 	= array();
		$conn		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$sql 	= 'SELECT count(1) FROM image_custom' . $where;
			
			$rs  	= mysql_query($sql, $conn);
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if(!empty($total)){				
				$sql  = 'SELECT width, height, extName, fileSize, name, description, id, md5, status, level, isAnimated ';
				$sql .= "FROM image_custom $where $order";
				
				if($discount > 0)
					$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $conn) or die("<br><br>Invalid query: " . $sql . mysql_error($conn));
				while($rs && $row = mysql_fetch_array($rs)){
					$dto = new ImageDto();
	
					$dto->width			= $row[0];
					$dto->height		= $row[1];
					$dto->extName		= $row[2];
					$dto->fileSize		= $row[3];
					$dto->name			= $row[4];
					$dto->description	= $row[5];
					$dto->id 			= $row[6];
					$dto->md5			= $row[7];
					$dto->status		= $row[8];
					$dto->level			= $row[9];
					$dto->isAnimated	= $row[10];
					
					$arrback[] = $dto;
				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);	
		
		return $arrback;
	}

	
	public function onClick($imageId)
	{
		$sql = "INSERT INTO image_custom SET r_clicked = 1, id = $imageId ON DUPLICATE KEY UPDATE r_clicked = r_clicked + 1";
		$this->update($this->DBTag, $sql);
	}
}
?>
